﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.Data
{

    internal sealed partial class DataCoreManager : GameFrameworkModule,IDataCoreManager
    {
        public Dictionary<string, DataBaseManager> m_DataManagers = new Dictionary<string, DataBaseManager>();


        /// <summary>
        /// 获取游戏框架模块优先级。
        /// </summary>
        /// <remarks>优先级较高的模块会优先轮询，并且关闭操作会后进行。</remarks>
        internal override int Priority
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 游戏框架模块轮询。
        /// </summary>
        /// <param name="elapseSeconds">逻辑流逝时间，以秒为单位。</param>
        /// <param name="realElapseSeconds">真实流逝时间，以秒为单位。</param>
        internal override void Update(float elapseSeconds, float realElapseSeconds)
        { }

        /// <summary>
        /// 关闭并清理游戏框架模块。
        /// </summary>
        internal override void Shutdown()
        {

        }


        /// <summary>
        /// 获取数据管理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>

        public T GetManager<T>() where T : DataBaseManager
        {
            Type interfaceType = typeof(T);
            if (!interfaceType.IsInterface)
            {
                //todo
                //throw new GameFrameworkException(Utility.Text.Format("You must get module by interface, but '{0}' is not.", interfaceType.FullName));
            }

            //if (!interfaceType.FullName.StartsWith("GameFramework.", StringComparison.Ordinal))
            //{
            //    //todo
            //    //throw new GameFrameworkException(Utility.Text.Format("You must get a Game Framework module, but '{0}' is not.", interfaceType.FullName));
            //}

            //string moduleName = Utility.Text.Format("{0}.{1}", interfaceType.Namespace, interfaceType.Name.Substring(1));
            string managerName = string.Format("{0}.{1}", interfaceType.Namespace, interfaceType.Name);
            //Type moduleType = Type.GetType(moduleName);
            Type managerType = System.Reflection.Assembly.Load("Assembly-CSharp").GetType(managerName);
            if (managerType == null)
            {
                //todo
                //throw new GameFrameworkException(Utility.Text.Format("Can not find Game Framework module type '{0}'.", moduleName));
            }

            return GetManager(managerType) as T;
        }

    
        /// <summary>
        /// 获取数据管理
        /// </summary>
        /// <param name="managerType"></param>
        /// <returns></returns>
        private DataBaseManager GetManager(Type managerType)
        {
            DataBaseManager manager = null;
            if (!m_DataManagers.TryGetValue(managerType.Name, out manager))
            {
                manager = CreateManager(managerType);
            }

            return manager;
        }

        /// <summary>
        /// 创建数据管理
        /// </summary>
        /// <param name="managerType"></param>
        /// <returns></returns>
        private DataBaseManager CreateManager(Type managerType)
        {
            DataBaseManager manager = (DataBaseManager)Activator.CreateInstance(managerType);
            if (manager == null)
            {

                //throw new GameFrameworkException(Utility.Text.Format("Can not create module '{0}'.", moduleType.FullName));
            }

            m_DataManagers.Add(managerType.Name, manager);

            return manager;
        }

   
    }
}
